<template>
  <view style="width: 100%; height: 100%">
    <section class="section">
      <view class="login-content">
        <view class="login-header">
          <img src="/static/image/img_03.png" />
        </view>
        <view class="login-title">登录/注册</view>
        <form
          class="login-form"
          action=""
          :model="form"
          ref="form"
          :rules="rules"
        >
          <view class="login-form-input">
            <img
              class="login-form-icon"
              src="static/image/icon_03.png"
              alt="手机号码"
            />
            <input
              type="number"
              id="phone"
              maxlength="11"
              name="account"
              placeholder="请输手机号码"
              v-model="form.phone"
            />
          </view>
          <view class="login-form-input login-form-code">
            <img
              class="login-form-icon"
              src="static/image/icon_02.png"
              alt="验证码"
            />
            <input
              type="number"
              name="code"
              id="code"
              placeholder="请输入验证码"
              v-model="form.code"
            />
            <button class="login-code-btn" type="button" @click="getCode" :disabled="isCountingDown">
              {{ isCountingDown ? `${countDown}秒` : '获取' }}
            </button>
          </view>
          <view class="login-form-btn">
            <button type="button" id="submit" @click="submitForm">
              登录或注册
            </button>
          </view>
          <view class="login-form-tips">
            <img src="static/image/icon_06.png" alt="提示" />
            <text>请输入<span>手机号码</span>自动检测新老账号</text>
          </view>

          <view class="login-form-agreement">
            <checkbox
              :checked="form.isChecked"
              class="custom-checkbox"
              @click="handleChange"
            />
            我同意<a href="javascript:void(0)" onclick="xieyi()">用户协议</a
            >，并遵守和承担相关法律法规
          </view>
        </form>
      </view>
    </section>
  </view>
</template>

<script>
	import { getToken, setToken } from '@/utils/auth'
	import { login, sendcode } from '@/api/index'

export default {
  onLoad() {
    uni.setNavigationBarTitle({
      title: '登录/注册'
    });
  },
  data() {
    return {
      isChecked: false,
      countDown: 0, // 倒计时秒数
      isCountingDown: false, // 是否正在倒计时
      form: {
        password: '',
        phone: '',
        token: '',
        code: '',
        isChecked: false
      },
      rules: {
        phone: [
          {
            required: true,
            message: '请输入手机号码',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              if (!value) {
                callback(new Error('请输入手机号码'));
              } else if (!/^1[3456789]\d{9}$/.test(value)) {
                callback(new Error('请输入正确的手机号码'));
              }
            }
          }
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  mounted() {
	  const token = getToken();
	  if (token) {
		uni.switchTab({
			url: "/pages/aindex/aindex"
		});
	  }
  },
  methods: {
    handleChange() {
      this.form.isChecked = !this.form.isChecked;
      console.log(this.isChecked, 'isChecked');
      console.log(this.password, 'password');
      console.log(this.phone, 'phone');
      console.log(this.token, 'token');
    },
    
    // 开始倒计时
    startCountDown() {
      this.countDown = 60;
      this.isCountingDown = true;
      
      const timer = setInterval(() => {
        this.countDown--;
        if (this.countDown <= 0) {
          clearInterval(timer);
          this.isCountingDown = false;
        }
      }, 1000);
    },
    
	// 获取验证码
    getCode() {
		// 如果正在倒计时，不执行操作
		if (this.isCountingDown) return;
		
		if (!this.form.phone) {
			uni.showToast({
			  title: '请输入手机号',
			  icon: 'warning',
			  duration: 1500
			});
			return;
		}
		
		// 验证手机号格式
		if (!/^1[3456789]\d{9}$/.test(this.form.phone)) {
			uni.showToast({
			  title: '请输入正确的手机号码',
			  icon: 'error',
			  duration: 1500
			});
			return;
		}
		
		const data = {
			phone: this.form.phone
		}
		sendcode(data).then(res => {
			console.log(1234, res)
			// 开始倒计时
			this.startCountDown();
		})
      // 走验证码接口
      uni.showToast({
        title: '获取验证码成功',
        icon: 'success',
        duration: 2000
      });
    },
    
    // 登录或注册
    submitForm() {
      if (!this.form.isChecked) {
        uni.showToast({
          title: "请勾选用户同意",
          icon: "error",
          duration: 2000
        });
        return;
      }
      // 表单校验
      const { phone, password, code, isChecked } = this.form;
	  
	  const data = {
	  	phone: this.form.phone,
		code: this.form.code
	  }
	  
	  login(data).then(res => {
		  console.log(55555, res)
		  const { code, data } = res;
		  if (code === 200) {
			  const { token } = data;
			  setToken(token)
			  setTimeout(() => {
				  uni.switchTab({
				  	url: "/pages/aindex/aindex"
				  });
			  }, 1000)
		  }
	  })

      //   走登录接口
      uni.showToast({
        title: "登录成功",
        icon: "success",
        duration: 2000
      });
    }
  }
}
</script>

<style lang="scss" scoped>
.login-content {
  background: url(/static/image/back_02.png) #ffffff center top no-repeat;
  background-size: 100%;
  min-height: 100vh;
}

.login-header {
  padding: 80px 0 20px;
  text-align: center;
}

.login-header span {
  width: 150px;
  height: 136px;
  background-image: url(/static/image/img_03.png);
  background-size: cover;
  display: inline-block;
}

.login-title {
  position: relative;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  color: #333;
  padding-bottom: 10px;
}

.login-title::after {
  position: absolute;
  bottom: 0;
  left: 50%;
  width: 26px;
  height: 4px;
  content: " ";
  background: #ec81a7;
  border-radius: 2px;
  margin-left: -13px;
}

.login-form {
  display: block;
  padding: 20px 30px;
}

.login-form-input {
  position: relative;
  background: #f5f5f5;
  height: 44px;
  border-radius: 22px;
  margin-top: 15px;
  padding-left: 44px;
  overflow: hidden;
}

.login-form-input:first-child {
  margin: 0;
}

.login-form-icon {
  width: 16px;
  height: 16px;
  position: absolute;
  top: 50%;
  left: 14px;
  margin-top: -8px;
}

.login-form-input input {
  width: 100%;
  height: 44px;
  border: none;
  padding: 0 15px 0 0;
  margin: 0;
  background: transparent;
  outline: none;
  font-size: 14px;
}

.login-form-input input::placeholder {
  font-size: 14px;
}

.login-form-password {
  padding-right: 44px;
}

.login-password-icon {
  position: absolute;
  top: 0;
  right: 0;
  width: 44px;
  height: 44px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.login-password-icon img {
  width: 16px;
  height: 16px;
  margin: 0;
}

.login-form-code {
  padding-right: 120px;
}

.login-form-code::after {
  position: absolute;
  content: " ";
  top: 12px;
  bottom: 12px;
  right: 120px;
  width: 1px;
  background: #ec81a7;
}
.login-code-btn {
  position: absolute;
  height: 44px;
  border: none;
  top: 0;
  right: 0;
  width: 120px;
  padding: 0;
  background: transparent;
  color: #ec81a7;
}
::v-deep uni-button.login-code-btn::after {
  border: none;
}
::v-deep uni-button.login-code-btn:disabled {
  background: transparent;
  color: #ccc;
}
.login-form-btn {
  margin-top: 30px;
}

.login-form-btn button {
  width: 100%;
  height: 44px;
  background: #ec81a7;
  color: white;
  border: none;
  border-radius: 22px;
  font-size: 16px;
}

.login-form-btn button:disabled {
  background: rgba(236, 129, 167, 0.2);
  color: #888;
}

.login-form-tips {
  padding: 20px 0;
  text-align: center;
  color: #666666;
}

.login-form-tips span {
  color: #ec81a7;
}

.login-form-tips img {
  margin: 0 5px 0 0;
  width: 16px;
  height: 16px;
}

.login-form-agreement {
  padding: 30px 10px;
  text-align: center;
  color: #333;
}

.login-form-agreement a {
  color: #4286c8;
  margin: 0 2px;
}

.login-form-checkbox {
  position: relative;
  height: 20px;
  width: 20px;
  margin-right: 5px;
}

.login-form-checkbox input[type="checkbox"] {
  opacity: 0.5;
  position: absolute;
  top: 0;
  right: 0;
  z-index: -1;
}

.login-form-checkbox i {
  position: absolute;
  top: -1px;
  left: 3px;
}

.login-form-checkbox label {
  vertical-align: top;
  display: inline-block;
  background: white;
  border: solid 2px #afafaf;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  box-sizing: border-box;
  color: white;
}
::v-deep .uni-button:after {
  border: none !important;
  border: 1px solid red;
}
.login-form-checkbox input[type="checkbox"]:checked + label {
  background: #ec81a7;
  border-color: #ec81a7;
}

.login-header {
  padding: 50px 0 20px;
  text-align: center;
  img {
    width: 150px;
    height: 136px;
    background-image: url(/static/image/img_03.png);
    background-size: cover;
    display: inline-block;
  }
}
.login-form-agreement {
  padding-top: 20px;
}

.custom-checkbox {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  padding: 0;
  border-radius: 50%;
  background-color: transparent;
}

::v-deep uni-checkbox .uni-checkbox-input {
  border-radius: 50%;
  width: 18px;
  height: 18px;
  margin-bottom: 5px;
}
</style>
